package org.itboys.mobile.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mongo.SundayApply;
import org.itboys.mobile.entity.mongo.SundayExpert;
import org.itboys.mobile.service.mongo.web.SundayExpertService;
import org.itboys.mobile.util.ResponseMessage;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author：贾琦磊
 * @Date：2018/8/14
 * @Description：专家
 *
 */
@Controller
@RequestMapping("/sunday/web/expert")
public class SundayExpertController extends BaseController{

    @Autowired
    private SundayExpertService expertService;
    @RequestMapping("/index" )
    public  String index(HttpServletRequest request,
                         Model model){
        return "/sunday/expert/index";
    }

    /**
     *
     * @param request
     * @return
     */
    @RequestMapping("/input")
    public String input(
            HttpServletRequest request,
            @RequestParam(value="id",required=true)Long id,
            Model model
    ){
        model.addAttribute("expert",expertService.getById(id)) ;
        return "/sunday/expert/input";
    }

    @RequestMapping("/save")
    public void save(HttpServletRequest request,
                     HttpServletResponse response,
                     @RequestParam(value="id") Long id,
                     @RequestParam(value="image",required=false) MultipartFile imageFile,
                     @RequestParam(value="name") String name
    ){
        try {
            expertService.save(id, name,  imageFile);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            String msg = " ";
            if (e.getClass().getSimpleName().equalsIgnoreCase("Error Exception")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/expert/save------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE, CommonConstants.FAIL);

        }
    }

    @RequestMapping("/select")
    public void select(HttpServletRequest request,HttpServletResponse response){
        try {
            PageResult<SundayExpert> result = expertService.select(request);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

    @RequestMapping("/delete")
    public void delete(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="id",required=true) Long id
    ){
        try {
            expertService.delete(id);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/expert/delete------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE, msg);
        }
    }
    @RequestMapping("/choose" )
    public  String choose(HttpServletRequest request,
                          @RequestParam(value="isSingle",required=true)Integer isSingle,//是否单选
                          @RequestParam(value="callBack",required=false)String callBack,//回调参数
                          @RequestParam(value="param",required=false)String param,//查询参数
                          Model model){
        model.addAttribute("param",param);
        model.addAttribute("callBack",callBack);
        if(isSingle==null){
            return "/sunday/expert/choose/choose_erro";
        }
        return "/sunday/expert/choose/choose_single";
    }
}
